<template>
	<el-dialog v-model="dialogFormVisible" :title='title' width="30%" center>
		<span v-if="title=='删除信息'" v-html="tips"></span>
		<span v-else-if="title=='恢复消息'" v-html="tips"></span>
		<span v-else v-html="tips"></span>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="operationMessage">
					确定
				</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
  import { reactive, ref, onBeforeUnmount } from 'vue';
  import { ElMessage } from 'element-plus';
  import { bus } from "@/utils/mitt.js";
  import { firstDelete, recover, deleteMessage } from '@/api/message.js';
	import { tracking } from '@/utils/operation.js';
	import { changeUserReadListButDelete, changeUserReadList, setAllDepartment, deleteAllDepartment } from '@/api/dep_msg.js';
	import { useMsg } from '@/store/message.js';

	const msgStore = useMsg()

  const title = ref()
	const tips = ref()
	const messageId = ref() // 消息ID
	const department = ref()

  bus.on('deleteMessageId', (row : any) => {
		title.value = '删除信息'
		tips.value = '您确定要删除这个公告吗？'
		messageId.value = row.id
		department.value = row.message_receipt_object
	})

	bus.on('renewID', (row : any) => {
		title.value = '恢复消息'
		tips.value = '您确定要恢复这个公告吗？'
		messageId.value =row.id
		department.value = row.message_receipt_object
	})

	bus.on('realDelete', (id : number) => {
		title.value = '彻底删除信息'
		tips.value = '请慎重操作！您确定要彻底删除这个公告吗？'
		messageId.value = id
	})
  
  const emit = defineEmits(['success'])
  const operationMessage = async () => {
    // 1. 删除信息
    if (title.value == '删除信息') {
			const res = await firstDelete(messageId.value)
			if (res.data.status == 0) {
				if(department.value === '全体成员'){
					await deleteAllDepartment(messageId.value)
					msgStore.returnReadList(localStorage.getItem('id'))// ??
				}else{
					await changeUserReadListButDelete(messageId.value, department.value)
					msgStore.returnReadList(localStorage.getItem('id')) //??
				}
				
				ElMessage({
					message: '删除公告成功',
					type: 'success',
				})
				tracking('消息', localStorage.getItem('name'), messageId.value, '高级')
				emit('success')
				dialogFormVisible.value = false
			} else {
				ElMessage.error('删除公告失败')
				dialogFormVisible.value = false
			}
		}

    // 2. 恢复消息
		if (title.value == '恢复消息') {
			const res = await recover(messageId.value)
			if (res.data.status == 0) {
				if(department.value === '全体成员'){
					await setAllDepartment(messageId.value)
					msgStore.returnReadList(localStorage.getItem('id'))// ??
				}else{
					await changeUserReadList(messageId.value, department.value)
					msgStore.returnReadList(localStorage.getItem('id')) // ??
				}
				
				ElMessage({
					message: '恢复公告成功',
					type: 'success',
				})
				emit('success')
				dialogFormVisible.value = false
			} else {
				ElMessage.error('恢复公告失败')
				dialogFormVisible.value = false
			}
		}

    // 3. 真正删除信息
		if (title.value == '彻底删除信息') {
			const res = await deleteMessage(messageId.value)
			if (res.data.status == 0) {
				ElMessage({
					message: '删除公告成功',
					type: 'success',
				})
				emit('success')
				dialogFormVisible.value = false
			} else {
				ElMessage.error('删除公告失败')
				dialogFormVisible.value = false
			}
		}
  }

  // ------------------------------------------------------------------------------弹窗开关
	const dialogFormVisible = ref(false)
  // 打开创建管理员的弹窗
  const open = () => {
    dialogFormVisible.value = true
  }
  defineExpose({
    open
  })
  onBeforeUnmount(() => {
		bus.all.clear()
	})
</script>
 
<style lang="scss" scoped>
  
</style>
